package io.renren.common.utils;

import org.joda.time.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具
 */
public class DateUtil extends DateUtils {
    public static final Logger log = LoggerFactory.getLogger(DateUtil.class);

    public static final String DATE_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_3 = "yyyyMMdd";
    public static final String DATE_FORMAT_4 = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_5 = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getCurrentDate() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }

    /**
     * ThreadLocal store thread safe SimpleDateFormat
     */
    private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat());

    public static Date getStr2Date(String date, String format) {
        SimpleDateFormat simpleDateFormat = DATE_FORMAT.get();
        simpleDateFormat.applyPattern(format);
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 返回两个时间段是否在一天内
     *
     * @param startTime
     * @param endTime
     * @param format
     */
    public static long dateDiff(String startTime, String endTime, String format) throws ParseException {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        //获得两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();

        return (diff / nd);
    }

    /**
     * 获取过去 minutes 分钟的时间
     *
     * @param minutes
     * @return
     */
    public static Date getPastMinutesDate(int minutes) {
        DateTime dt = new DateTime().minusMinutes(minutes);
        return dt.toDate();
    }

    /**
     * date format by format string
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDate2Str(Date date, String format) {
        DATE_FORMAT.get().applyPattern(format);
        return DATE_FORMAT.get().format(date);
    }

    /**
     * 计算 date1 与 date2 相差的秒数
     * @param date1
     * @param date2
     * @return
     */
    public static int secondsBetween(Date date1, Date date2) {
        DateTime dt1 = new DateTime(date1);
        DateTime dt2 = new DateTime(date2);
        int seconds = 0;
        // 相差天数
        int days = Days.daysBetween(dt1, dt2).getDays();
        if (days > 0) {
            seconds += days * 24 * 60 * 60;
        }
        // 小时数
        int hours = Hours.hoursBetween(dt1, dt2).getHours() % 24;
        if (hours > 0) {
            seconds += hours * 60 * 60;
        }
        // 分钟数
        int minutes = Minutes.minutesBetween(dt1, dt2).getMinutes() % 60;
        if (minutes > 0) {
            seconds += minutes * 60;
        }
        // 秒数
        int sec = Seconds.secondsBetween(dt1, dt2).getSeconds() % 60;
        if (sec > 0) {
            seconds += sec;
        }

        return seconds;
    }

    // 返回 yyyyMMdd 当前日期
    public static String getCurDay() {
        return getDate2Str(new Date(), DATE_FORMAT_3);
    }

    public static void main(String[] args) {
        String date = "2017-02-10 12:00:00";
//        DateTime parse = DateTime.parse(date);
//        System.out.println(parse.toString("yyyy-MM-dd"));

        /*SimpleDateFormat simpleDateFormat = DATE_FORMAT.get();
        simpleDateFormat.applyPattern(DATE_FORMAT_2);
        String format = simpleDateFormat.format(date);*/

        System.out.println(getStr2Date(date, DATE_FORMAT_1));

        //System.out.println(DateUtil.getDate2Str(DateUtil.addHours(new Date(), 2), DateUtil.DATE_FORMAT_1));

        test2();
    }


    private static void test2() {
        DateTime dt1 = new DateTime(getStr2Date("2017-02-10 14:23:34", DATE_FORMAT_1));
        DateTime dt2 = new DateTime(getStr2Date("2017-02-10 13:23:34", DATE_FORMAT_1));

        System.out.print("时间相差：");
        System.out.print(Days.daysBetween(dt1, dt2).getDays() + " 天 ");
        System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " 小时 ");
        System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " 分钟 ");
        System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " 秒.");

        System.out.println("=====" + secondsBetween(
                getStr2Date("2017-02-10 14:20:38", DATE_FORMAT_1),
                getStr2Date("2017-02-10 14:43:39", DATE_FORMAT_1))
        );
    }

}
